import {
  secondsToHourMinuteSecond
} from '../../utils/util'

Page({
  data: {
    debug: "Debug",
    item: null,
    songLengthString: '', // Can't call function from wxml. Have to pre-calculate.
    isPlayingAudio: false,
    isLoadingMusic: false
  },
  onShareAppMessage() {
    const path = '/pages/song/index?item=' + encodeURIComponent(JSON.stringify(this.data.item))
    this.setData({
      debug: path
    })
    return {
      title: this.data.item.name,
      path: path,
      imageUrl: this.data.item.url
    }
  },
  onLoad(options) {    
    this.setData({
      debug: options
    })

    if(options.item){
      const item = JSON.parse(decodeURIComponent(options.item))
      this.setData({
        item: item,
        songLengthString: secondsToHourMinuteSecond(item.metadata.duration)
      })
      getApp().globalData.needRefresh = true;
    }else{
      const eventChannel = this.getOpenerEventChannel();
      eventChannel.on("acceptSongItem", (item) => {
        this.setData({
          item: item,
          songLengthString: secondsToHourMinuteSecond(item.metadata.duration)
        })
      })
    }
  },
  onBackTap(e) {
    const pages = getCurrentPages();
    if (pages.length > 1) {
      wx.navigateBack({
        delta: 1
      });
    } else {
      wx.switchTab({
        url: '/pages/home/index'
      });
    }
  },
  innerAudioContext: null,
  getAudioContext() {
    if (!this.innerAudioContext) {
      this.innerAudioContext = wx.createInnerAudioContext();
      let _this = this
      this.innerAudioContext.onWaiting(() => {
        _this.setData({
          isLoadingMusic: true
        })
      })
      this.innerAudioContext.onPlay(() => {
        _this.setData({
          isLoadingMusic: false
        })
      })
      this.innerAudioContext.onEnded(() => {
        _this.setData({
          isPlayingAudio: false
        })
      })
    }
    return this.innerAudioContext;
  },
  onPlayMusic(e) {
    const innerAudioCtx = this.getAudioContext();
    console.log(innerAudioCtx)
    if (undefined === innerAudioCtx.paused || true === innerAudioCtx.paused) {
      innerAudioCtx.src = this.data.item.versions[0].previewURL;
      innerAudioCtx.play();
      this.setData({
        isPlayingAudio: true
      })
    } else {
      innerAudioCtx.stop();
      this.setData({
        isPlayingAudio: false
      })
    }
  },
  onHide() {
    if (this.innerAudioContext) {
      this.innerAudioContext.stop()
    }
  },
  marshalDiffItem(diffItem){
    return JSON.stringify(diffItem)
  },
  onPlayNote(e) {
    console.log("Opening page: playNote");
    const item = this.data.item;
    wx.navigateTo({
      url: '/pages/playNote/index',
      success(res) {
        const dataset = e.currentTarget.dataset
        res.eventChannel.emit('acceptSongItem', {
          item: item,
          diffObj: dataset
        })
      }
    })
  },
  onUnload() {
    if (this.innerAudioContext) {
      this.innerAudioContext.stop()
      this.innerAudioContext.destroy()
      this.innerAudioContext = null
    }
  },
  onCollectMusic(e) {
    wx.showToast({
      title: '还没有实现',
      icon: 'none'
    });
  },
  onCopyURL(e){
    wx.setClipboardData({
      data: this.data.item.versions[0].downloadURL,
      success: () => {
        wx.showToast({
          title: '已复制',
          icon: 'success'
        });
      }
    });
  },
  clickedMapper(e){
    console.log("Mapper id:" + e.currentTarget.dataset.id)
    wx.navigateTo({
      url: '/pages/mapper/index?id=' + e.currentTarget.dataset.id,
    })
  }
})